/******************************************************************************/
package jamie.org.sil.www.usxUtils;

import java.util.Iterator;
import java.util.NoSuchElementException;


/******************************************************************************/
/**
 * An iterator which runs over all scripture files mentioned in the book
 * book assembly configuration file.
 * <p>
 * 
 * Note that this class interacts very closely with {@link BookAssembly}, qv.
 * 
 * @author Jamie
 */

public class BookAssemblyScriptureFilesIterator implements Iterator<BookElement>
{
  public BookAssemblyScriptureFilesIterator () {}

  public BookAssemblyScriptureFilesIterator (boolean checkInputFileExists) { m_CheckInputFileExists = checkInputFileExists; }

  @Override public boolean hasNext ()
  {
    for (int i = m_Ix + 1; i < BookAssembly.instance().getBookElements().size(); ++i)
    {
      BookElement be = BookAssembly.instance().getBookElements().get(i);
      if (!"scripture".equals(BookAssembly.instance().getBookElements().get(i).getType())) continue;
      if (be.getInputFileExists()) return true;
    }
      
    return false;
  }
    
  @Override public BookElement next () throws NoSuchElementException
  {
    for (int i = m_Ix + 1; i < BookAssembly.instance().getBookElements().size(); ++i)
    {
      BookElement be = BookAssembly.instance().getBookElements().get(i);
      if (!"scripture".equals(BookAssembly.instance().getBookElements().get(i).getType())) continue;
      if (m_CheckInputFileExists && !be.getInputFileExists()) continue;
      m_Ix = i;
      return be;
      }
      
    throw new NoSuchElementException();
  }
    
  @Override public void remove () {}
   
  private boolean m_CheckInputFileExists = false;
  private int m_Ix = -1;
}
